//
//  WXPKPrinterEffectView.m
//  打字效果
//
//  Created by tianlong on 2018/3/30.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "WXPKPrinterEffectView.h"
#import "WXTimerHelper.h"

@interface WXPKPrinterEffectView ()
/** 记录当前打印的文字的索引 */
@property (assign, nonatomic) NSInteger curIndex;
@property (strong, nonatomic) NSThread *thread;
@property (strong, nonatomic) WXTimerHelper *helper;
/** 默认打字的速度为0.1秒打一个字 */
@property (assign, nonatomic) NSTimeInterval interval;
@property (assign, nonatomic) BOOL usingAnimate;
@property (assign, nonatomic) TimerState timeState;
@end

@implementation WXPKPrinterEffectView

- (instancetype)initWithUsingAnimate:(BOOL)yesOrNo interval:(NSTimeInterval)interval {
    self = [super init];
    if (self) {
        self.editable = NO;
        self.curIndex = 0;
        self.usingAnimate = yesOrNo;
        self.interval = (interval >= 0 ? interval : 0.1);
    }
    return self;
}

- (void)setText:(NSString *)text{
    if (self.usingAnimate) {
         [super setText:nil];
    }else{
         [super setText:text];
    }
    self.animteText = text;
}

- (WXTimerHelper *)helper{
    if (!_helper) {
        _helper = [WXTimerHelper scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(animateText) userInfo:nil repeats:YES];
    }
    return _helper;
}

#pragma mark - 动画打字的核心代码
- (void)animateText {
    NSInteger loc = self.curIndex + 1;
    if(loc > self.animteText.length){
        [self stopTextAnimate];
        if (self.animationCompletion) {
            self.animationCompletion();
        }
        return;
    }
    NSString *sub = [self.animteText substringWithRange:NSMakeRange(0, loc)];
    self.curIndex++;
    
    // 设置字体属性
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    if (self.font) {
        [dictM setValue:self.font forKey:NSFontAttributeName];
    }else{
        UIFont *defaultFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        UIFontDescriptor *ctfFont = defaultFont.fontDescriptor;
        NSNumber *fontString = [ctfFont objectForKey:@"NSFontSizeAttribute"];
        if (fontString) {
            [dictM setValue:[UIFont systemFontOfSize:fontString.intValue] forKey:NSFontAttributeName];
        }
    }
    if (self.textColor) {
        [dictM setValue:self.textColor forKey:NSForegroundColorAttributeName];
    }
    self.attributedText = [[NSAttributedString alloc] initWithString:sub attributes:dictM];
    [self scrollRangeToVisible:NSMakeRange(self.text.length, 1)];
    self.layoutManager.allowsNonContiguousLayout = NO;
    
    // 这个判断：打字速度是随机的几个值通过睡线程实现
    if (self.randomSpeed) {
        int ran = arc4random_uniform(10)+1;
        [NSThread sleepForTimeInterval:ran*0.01];
    }
}


- (void)resumeTextAnimate {
    if (self.interval == 0 && self.usingAnimate) {
        self.text = self.animteText;
    }else{
        if (self.usingAnimate) {
            [self.helper resume];
        }
    }
}

- (void)pauseTextAnimate {
    if (self.usingAnimate) {
        [self.helper pause];
    }
}

- (void)stopTextAnimate {
    if (self.usingAnimate) {
        [self.helper stop];
        self.curIndex = 0;
    }
}

@end
